
%params for imshow layout
graph_size=5;
location=1;

%read the original picture
img_raw=imread("b1(1).bmp");
% location=location+1;

subplot(graph_size,graph_size,location);
imshow(img_raw),title("原始图像")
% try to pick out the purple pixels
img_raw_r=img_raw(:,:,1);
img_raw_g=img_raw(:,:,2);
img_raw_b=img_raw(:,:,3);

imbt=1.*(img_raw_r<200);
imbt=imbt.*(img_raw_g<150);
imbt=imbt.*(img_raw_b>200);

location=location+1;
subplot(graph_size,graph_size,location);
imshow(imbt),title("提取通道")





% conversions testing with hsv
img_hsv=rgb2hsv(img_raw);
location=location+1;
subplot(graph_size,graph_size,location);
imshow(img_hsv);
% img_vec=rgb2gray(img_raw);
for index=1:3
    location=location+1;
    subplot(graph_size,graph_size,location);
    imshow(img_hsv(:,:,index)),title(index+"-hsv");
end

img_vec=rgb2gray(img_raw);
img_vec=255-img_vec;
location=location+1;
subplot(graph_size,graph_size,location);
imshow(img_vec),title("提取hsv分量");
img_vec=imfill(img_vec,'holes');

location=location+1;
subplot(graph_size,graph_size,location);
imshow(img_vec),title("填充空洞");


img_grad=imfilter(img_vec,[-1,1;1,-1]);
location=location+1;
subplot(graph_size,graph_size,location);
imshow(img_grad),title("锐化");

% pick the vector that represents best 
img_vec=255.*(img_hsv(:,:,2));
img_vec=uint8(255.*(img_vec/max(max(img_vec))));
% disp(img_vec(1,1));
location=location+1;
subplot(graph_size,graph_size,location);
imhist(img_vec);

% binarize using watershed
thh=otsuthresh(imhist(img_vec));
imb=imbinarize(img_vec,thh);

%open
location=location+1;
subplot(graph_size,graph_size,location);
imb=imerode(imb,strel("sphere",4));
imshow(imb);

% imb=uint8(img_vec)>65;
% imb=imfill(imb,"holes");
location=location+1;
subplot(graph_size,graph_size,location);
imshow(imb),title("门槛二值化");
mask=imb;

location=location+1;
subplot(graph_size,graph_size,location);
imshow(~imb),title("取反")

imDists=bwdist(~imb);
location=location+1;
subplot(graph_size,graph_size,location);
imshow(imDists,[]),title("距离")

imDists=-imDists;
location=location+1;
subplot(graph_size,graph_size,location);
imshow(imDists,[]),title("距离求反")

im_watershed = watershed(imDists,8);
im_watershed(~imb) = 0;

rgb = label2rgb(im_watershed,'jet',[.5 .5 .5]);
location=location+1;
subplot(graph_size,graph_size,location);
imshow(rgb)
title('分水岭')
imwrite(rgb,"temp2.png");



%apply the rgb vector test result
imb=imbt;

%open
location=location+1;
subplot(graph_size,graph_size,location);
imb=imerode(imb,strel("sphere",6));
imshow(imb);

% %erode
% template_strel = strel("sphere",8);
% ime=imerode(imb,template_strel);
% location=location+1;
% subplot(graph_size,graph_size,location);
% imshow(ime);

im_morph=bwmorph(imb,'skel',Inf);
location=location+1;
subplot(graph_size,graph_size,location);
imshow(im_morph);

%select region using binarized as a mask
mask_rgb=img_raw;
mask_rgb(:,:,1)=mask;
mask_rgb(:,:,3)=mask;
mask_rgb(:,:,2)=mask;
masked=mask_rgb.*img_raw;
location=location+1;
subplot(graph_size,graph_size,location);
imshow(masked);

location=location+1;
subplot(graph_size,graph_size,location);
imhist(masked);

im_watershed = watershed(masked);
im_watershed(~bw) = 0;
% disp(size(im_watershed));
max_num=max(max(max(max(im_watershed))));
disp("max:"+max_num)

% im_watershed=uint8(double(masked)./double(max_num).*255.0);

location=location+1;
subplot(graph_size,graph_size,location);
imshow(uint8(im_watershed)),title("watershed");


